/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package br.com.site.actions;

import java.util.Date;
import java.util.List;

import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import sun.awt.AppContext;

import com.opensymphony.xwork2.ActionContext;

import br.com.site.model.dao.DAOException;

import br.com.site.model.entity.Usuario;

@ParentPackage(value = "fvpAdmin")
public class ListarUsuarios extends Listagem<Usuario> {

	private static final long serialVersionUID = -7133848243722723891L;

	private static final long nivel = 3;

	private String tipo;

	private Date dtLogsApartir;

	private Date dtLogsDepoisde;

	@Action(value = "/listarusuarios", results = {
			@Result(location = "listarUsuarios.jsp", name = SUCCESS),
			@Result(location = "loginsucess.jsp", name = BLOQUEAR),
			@Result(type = "redirect", location = "/error", name = ERROR),
			@Result(type = "redirect", location = "/login", name = LOGIN) })
	public String execute() throws Exception {
		clearErrorsAndMessages();
		if (verAcesso()) {

			try {
				init();
				nextPage();

			} catch (DAOException e) {

				/*
				 * addActionError(e.getMessage()); return ERROR;
				 */
				String error = "***ListarMural*** <br>" + e.toString();
				getSessao().put("myerror", error);
				return ERROR;
			}

			return SUCCESS;

		}
		addActionError("Acesso Negado!");
		return BLOQUEAR;
	}

	private void init() {
		int pageSize = 1;
		this.setPageatual(-1);
	}

	public String getTipo() {
		return tipo;
	}

	public void setTipo(String tipo) {
		this.tipo = tipo;
	}

	public Date getDtLogsApartir() {
		return dtLogsApartir;
	}

	public void setDtLogsApartir(Date dtLogsApartir) {
		this.dtLogsApartir = dtLogsApartir;
	}

	public Date getDtLogsDepoisde() {
		return dtLogsDepoisde;
	}

	public void setDtLogsDepoisde(Date dtLogsDepoisde) {
		this.dtLogsDepoisde = dtLogsDepoisde;
	}

	@Override
	public List<Usuario> listar() throws Exception {
		// TODO Auto-generated method stub
		// return super.listar(i, nelementos);
		return Usuario.find(getPageatual() * getPagesize(), getPagesize());

		/*
		 * Teste do criteria Usuario user = new Usuario();
		 * user.setNome("rytry"); user.setAtivo(true); return
		 * Usuario.findByCriteria(user);
		 */
	}

	@Override
	public Long getNivelMinAcesso() {
		return nivel;
	}

	@Override
	public boolean verAcesso() {
		return getMe().isAdministrador();
	}
	
	

}
